/* 
    Squirrel open project
    Copyright (C) 2012  Elias Aran Paul Farhan
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <squirrel/gameobject.hpp>

GameObject::GameObject(std::string filename, std::pair<int, int> pos, std::pair<int, int> size)
{
	this->filename = filename;
	this->pos = pos;
	this->size = size;
	image = 0;
}
GameObject::~GameObject()
{
	for(std::vector<Box*>::iterator it = boxes.begin(); it != boxes.end(); it++)
	{
		delete (*it);
	}
	
}
void GameObject::setPosition(std::pair<int, int> newpos)
{
	pos = newpos;
}
void GameObject::setImage(GLuint img)
{
	image = img;
}

void GameObject::addBox(Box* b)
{
	boxes.push_back(b);
}
std::vector<Box*>* GameObject::getBoxes()
{
	return &(this->boxes);
}
void GameObject::setSize(Point newsize)
{
	size = newsize;
}
void GameObject::drawObject(Point local_pos)
{
	//It is done without using glQuads for compatibility with OpenGL ES
	if(image != 0)
	{
		GLfloat vertices[] = 
		{
			0.0f,0.0f,(GLfloat)this->size.first,
			0.0f,0.0f,(GLfloat)this->size.second,
			(GLfloat)this->size.first,(GLfloat)this->size.second,
		};

		GLfloat tex_indices[] =
		{
			0.0f, 0.0f,
			1.0f, 0.0f,
			0.0f, 1.0f,
			1.0f, 1.0f,
		};
		
		GLfloat colors[] =
		{
			1.0f, 1.0f, 1.0f, 1.0f,
			1.0f, 1.0f, 1.0f, 1.0f,
			1.0f, 1.0f, 1.0f, 1.0f,
			1.0f, 1.0f, 1.0f, 1.0f,
		};
		
		glBindTexture(GL_TEXTURE_2D, this->image);

		glEnableClientState(GL_VERTEX_ARRAY);
		glEnableClientState(GL_COLOR_ARRAY);
		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
			
		glVertexPointer(2, GL_FLOAT, 0, vertices);
		glColorPointer (4, GL_FLOAT, 0, colors);
		glTexCoordPointer(2, GL_FLOAT, 0, tex_indices);
		
		glTranslatef(local_pos.first, local_pos.second, 0);
		
		glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
		
		glTranslatef(-local_pos.first, -local_pos.second, 0);
		glDisableClientState(GL_VERTEX_ARRAY);
		glDisableClientState(GL_COLOR_ARRAY);
		glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	}
}
void GameObject::setSizebyRatio(int ratio)
{
	this->size = std::make_pair(this->size.first/ratio, this->size.second/ratio);
}
Point GameObject::getPosition()
{
	return pos;
}
Point GameObject::getSize()
{
	return size;
}
